package com.gaber.polymorphic;

/**
 * @author gaber
 * @date 2023/7/11 15:34:45
 * @description TODO
 */
public class Test {
    public static void main(String[] args) {
//        show(new Cat()); //以Cat对象调用show方法
//        show(new Dog()); //以Dog对象调用show方法

        Animal c = new Cat(); // 向上转型
        c.eat();  // 调用的是 Cat 的 eat

        Cat c1 = (Cat) c; // 向下转型
        c1.work(); // 调用的是 Cat 的 eat

    }

    public static void show(Animal a) {
        a.eat();
        if(a instanceof Cat) {
            Cat c = (Cat) a;
            c.work();
        } else if (a instanceof Dog) {
            Dog c = (Dog) a;
            c.work();
        }
    }

}
abstract class Animal {
    abstract void eat();
}

class Cat extends Animal {
    public void eat() {
        System.out.println("吃鱼");
    }

    public void work() {
        System.out.println("抓老鼠");
    }
}

class Dog extends Animal {
    public void eat() {
        System.out.println("吃骨头");
    }
    public void work() {
        System.out.println("看家");
    }
}
